﻿#define _CRT_SECURE_NO_WARNINGS 1

/*有一个数字矩阵，矩阵的每行从左到右是递增的，矩阵从上到下是递增的，
请编写程序在这样的矩阵中查找某个数字是否存在。*/
#include<stdio.h>

#define row 3
#define col 4

int main()
{
	int n = 0;
	int i = 0;
	int tag = 0;
	//初始化一个符合条件的数组
	int arr[row][col] = { {1,2,3,4},{2,3,4,5},{3,4,5,6} };
	//输入要查询的数字
	scanf("%d", &n);
	//设置左右索引进行查询
	for (i = 0; i < row; i++)
	{
		int left = 0;
		int right = col;
		while (left < right)
		{
			if (arr[i][(left + right) / 2] < n)
				left = (left + right) / 2 + 1;
			else if (arr[i][(left + right) / 2] > n)
				right = (left + right) / 2 - 1;
			else if (arr[i][(left + right) / 2] == n)
			{
				tag = 1;
				printf("存在%d,它位于矩阵第%d行第%d列", n, i + 1, (left + right) / 2 + 1);
				putchar('\n');
				break;
			}
		}
		if (tag == 1)
			break;
		if (left >= right && i == row - 1)
			printf("%d不存在该矩阵中", n);
	}

	return 0;
}